package com.tw.marsrover;

public class Rover {
    private Position position;
    private Board board;

    public Rover(Position initialPosition, Board board) {
        this.position = initialPosition;
        this.board = board;
    }

    public Position getPosition(){
        return position;
    }

    public String getDirection() {
        return position.getDirection();
    }

    public void turnLeft() {
        position.turnLeft();
    }

    public void turnRight() {
        position.turnRight();
    }

    public void moveAhead() {
        Position newPosition = position.moveAhead();

        if(boardContains(newPosition))
            this.position = newPosition;

        else
            throw new RuntimeException("Invalid position");


    }

    private boolean boardContains(Position position) {
        return (position.getXCoordinate() < board.getLength()) &&
          (position.getYCoordinate() < board.getWidth());
    }
}
